class RankListPanel extends BasePanel
{
    private scrollView = new egret.ScrollView();   
    private userInfo:any = null;
    private fromPageIndex:number = 0;
    private pageCount:number = 6;
    private totalPageCount:number = 0;
    private dataList:Array<any> = [];
    private rankItemList:Array<RankItem> = [];
    private mineRankItem:RankItem;
    public constructor(data:any,index: number)
    {
        super(data,index);
        this.userInfo = data.mine;
        this.data = data;
        this.addEventListener(egret.Event.ADDED_TO_STAGE,this.createChildren,this);
    }
    public createChildren()
    {   
        this.dataList = GlobalData.friendDataList;
        this.totalPageCount = Math.floor((this.dataList.length+this.pageCount-1)/this.pageCount);
        this.updateDisplayInfo();
        this.loadRandData();
    }
    private loadRandData()
    {
        // 加载排行榜数据
        GlobalData.getAllFriendData(this.userInfo,this.showRank.bind(this));
    }
    public showNextPage(page:number){
        this.fromPageIndex += page;
        if(this.fromPageIndex<0){
            this.fromPageIndex = 0;
            return;
        }
        if(this.fromPageIndex>=this.totalPageCount){
            this.fromPageIndex = this.totalPageCount-1;
            return;
        }
        this.updateDisplayInfo();
    }
    /**
     * 将排行榜数据展示出来
     */
    private showRank(success:boolean,list: Array<any>) {
        // 没有数据内容不处理
        if(!success || !list || list.length==0){
            return;
        }
        this.dataList = list;
        this.totalPageCount = Math.floor((this.dataList.length+this.pageCount-1)/this.pageCount);
        this.updateDisplayInfo();
        
    }
    private updateDisplayInfo()
    {
        let listRect:any = this.data.listRect;
        // 清空之前显示的数据
        let preCount = this.rankItemList.length;
        for(let i=0;i<preCount;i++){
            let preRankIten = this.rankItemList.pop();
            if(preRankIten && preRankIten.parent){
                preRankIten.clearItem();
                preRankIten.parent.removeChild(preRankIten);
            }
        }
        this.rankItemList = [];
        let fromX = listRect.x;
        let fromY = listRect.y;
        let itemHeight = listRect.height/this.pageCount;
        let itemWidth = listRect.width;
        for(let i=this.fromPageIndex*this.pageCount;i<(this.fromPageIndex+1)*this.pageCount;i++)
        {
            if(i>=this.dataList.length){
                continue;
            }
            var dataItem = this.dataList[i];
            let rankItem = new RankItem(i,dataItem,itemWidth,itemHeight);
            rankItem.x = fromX;
            rankItem.y = fromY+itemHeight*(i-this.fromPageIndex*this.pageCount);
            // rankItem.width = itemWidth;
            // rankItem.height = itemHeight;
            
            this.addChild(rankItem);
            this.rankItemList.push(rankItem);
            
        }
       if(!this.mineRankItem){
           // 获取当前用户的信息
           let mineDataItem:any = null;
           for(let i=0;i<this.dataList.length;i++){
               let dataItem = this.dataList[i];
               if(dataItem.id == this.userInfo.id){
                    mineDataItem = dataItem;
                }
           }
           if(!mineDataItem)return;
           let mineRect:any = this.data.mineRect;
            // 底部展示自己的信息
            let mineRankItem = new RankItem(0, mineDataItem,mineRect.width, mineRect.height,true);
            mineRankItem.x = mineRect.x;
            mineRankItem.y = mineRect.y;
            this.addChild(mineRankItem);
            this.mineRankItem = mineRankItem;
       }
        
    }
}